package com.cskaoyan.service;

import com.cskaoyan.bean.MarketSystem;
import com.cskaoyan.bean.MarketSystemExample;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.mapper.MarketSystemMapper;
import com.cskaoyan.util.MyBatisUtil;
import com.sun.javafx.collections.MappingChange;
import org.apache.ibatis.session.SqlSession;

import java.util.*;

/**
 * @author Ramsey
 * @date 2023年05月30日 11:35
 */
public class SystemConfigServiceImpl implements SystemConfigService {
    @Override
    public Map<String, String> listMall() {
        System.out.println("systemConfigService.listMall();方法");
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        //
        MarketSystemExample example = new MarketSystemExample();
        MarketSystemExample.Criteria criteria = example.createCriteria();
        criteria.andKeyNameLike("market_mall_" + "%");
        MarketSystemMapper mapper = sqlSession.getMapper(MarketSystemMapper.class);
        List<MarketSystem> marketSystems = mapper.selectByExample(example);
        //构造一个map
        Map<String, String> map = new HashMap<>();
        //一一赋值
        for (MarketSystem marketSystem : marketSystems) {
            System.out.println(marketSystem);
            String keyName = marketSystem.getKeyName();
            String keyValue = marketSystem.getKeyValue();
            map.put(keyName, keyValue);
        }
        return map;
    }

    @Override
    public void updateConfig(Map<String, String> data) {
        //获取参数
        System.out.println();
        System.out.println("systemConfigService.updateConfig(data);方法");
        String market_mall_address = data.get("market_mall_address");
        String market_mall_latitude = data.get("market_mall_latitude");
        String market_mall_longitude = data.get("market_mall_longitude");
        String market_mall_name = data.get("market_mall_name");
        String market_mall_phone = data.get("market_mall_phone");
        String market_mall_qq = data.get("market_mall_qq");

        System.out.println(data);

        //开始改
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketSystemMapper mapper = sqlSession.getMapper(MarketSystemMapper.class);
        mapper.updateByNameAndValue("market_mall_address", market_mall_address);
        mapper.updateByNameAndValue("market_mall_latitude", market_mall_latitude);
        mapper.updateByNameAndValue("market_mall_longitude", market_mall_longitude);
        mapper.updateByNameAndValue("market_mall_name", market_mall_name);
        mapper.updateByNameAndValue("market_mall_phone", market_mall_phone);
        mapper.updateByNameAndValue("market_mall_qq", market_mall_qq);
    }

    //新增的
    @Override
    public BaseRespVo expressGet() {
        // 从数据库读取配置信息，返回给浏览器
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketSystemMapper systemMapper = sqlSession.getMapper(MarketSystemMapper.class);
        // 免运费最低消费金额
        MarketSystem freightMin = systemMapper.selectByPrimaryKey(6);
        String freightMinName = freightMin.getKeyName();
        String freightMinValue = freightMin.getKeyValue();
        // 快递费用
        MarketSystem freightValue = systemMapper.selectByPrimaryKey(8);
        String freightValueName = freightValue.getKeyName();
        String freightValueValue = freightValue.getKeyValue();
        sqlSession.close();

        // 需要一个对象的成员变量是：免运费最低消费消费、运费
        Map<String, String> map = new HashMap<>();
        map.put(freightMinName, freightMinValue);
        map.put(freightValueName, freightValueValue);

        return BaseRespVo.ok(map);
    }

    @Override
    public BaseRespVo orderGet() {
        // 从数据库读取配置信息，返回给浏览器
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketSystemMapper systemMapper = sqlSession.getMapper(MarketSystemMapper.class);
        // 订单保留时长
        MarketSystem orderUnpaid = systemMapper.selectByPrimaryKey(1);
        String unpaidKey = orderUnpaid.getKeyName();
        String unpaidName = orderUnpaid.getKeyValue();
        // 自动收货时长
        MarketSystem orderUnconfirm = systemMapper.selectByPrimaryKey(4);
        String unconfirmKey = orderUnconfirm.getKeyName();
        String unconfirmName = orderUnconfirm.getKeyValue();
        // 自动评价时长
        MarketSystem orderComment = systemMapper.selectByPrimaryKey(11);
        String commentKey = orderComment.getKeyName();
        String commentValue = orderComment.getKeyValue();

        // 关闭sqlSession
        sqlSession.close();

        // 对象的成员变量是：订单保留时长、自动收货时长、自动评价时长
        Map<String, String> map = new HashMap<>();
        map.put(unpaidKey, unpaidName);
        map.put(unconfirmKey, unconfirmName);
        map.put(commentKey, commentValue);

        return BaseRespVo.ok(map);
    }

    //
    @Override
    public BaseRespVo wxGet() {
        // 从数据库读取配置信息，返回给浏览器
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketSystemMapper systemMapper = sqlSession.getMapper(MarketSystemMapper.class);
        // 新品首发栏目商品显示数量
        MarketSystem wxIndexNew = systemMapper.selectByPrimaryKey(2);
        String indexNewKey = wxIndexNew.getKeyName();
        String indexNewValue = wxIndexNew.getKeyValue();
        // 商品分享功能
        MarketSystem wxShare = systemMapper.selectByPrimaryKey(5);
        String shareKey = wxShare.getKeyName();
        String shareValue = wxShare.getKeyValue();
        // 人气推荐栏目商品显示数量
        MarketSystem wxIndexHot = systemMapper.selectByPrimaryKey(10);
        String indexHotKey = wxIndexHot.getKeyName();
        String indexHotValue = wxIndexHot.getKeyValue();
        // 分类栏目商品显示数量
        MarketSystem wxCatlogGoods = systemMapper.selectByPrimaryKey(12);
        String catlogGoodsKey = wxCatlogGoods.getKeyName();
        String catlogGoodsValue = wxCatlogGoods.getKeyValue();
        // 分类栏目显示数量
        MarketSystem wxCatlogList = systemMapper.selectByPrimaryKey(15);
        String catlogListKey = wxCatlogList.getKeyName();
        String catlogListValue = wxCatlogList.getKeyValue();
        // 品牌制造商直供栏目品牌商显示数量
        MarketSystem wxIndexBrand = systemMapper.selectByPrimaryKey(17);
        String indexBrandKey = wxIndexBrand.getKeyName();
        String indexBrandValue = wxIndexBrand.getKeyValue();
        //
        MarketSystem wxIndexTopic = systemMapper.selectByPrimaryKey(18);
        String indexTopicKey = wxIndexTopic.getKeyName();
        String indexTopicValue = wxIndexTopic.getKeyValue();

        // 关闭sqlSession
        sqlSession.close();

        // 对象的成员变量各项系统配置
        Map<String, String> map = new HashMap<>();
        map.put(indexNewKey, indexNewValue);
        map.put(shareKey, shareValue);
        map.put(indexHotKey, indexHotValue);
        map.put(catlogGoodsKey, catlogGoodsValue);
        map.put(catlogListKey, catlogListValue);
        map.put(indexBrandKey, indexBrandValue);
        map.put(indexTopicKey, indexTopicValue);

        return BaseRespVo.ok(map);
    }

    @Override
    public BaseRespVo configPost(Map<String, Object> map) {
        // 获得所有的key
        List<String> keyList = new ArrayList<>();
        for (String key : map.keySet()) {
            keyList.add(key);
        }

        // 获取需要更改的系统配置
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketSystemMapper systemMapper = sqlSession.getMapper(MarketSystemMapper.class);
        MarketSystemExample systemExample = new MarketSystemExample();
        systemExample.createCriteria().andKeyNameIn(keyList);
        List<MarketSystem> systems = systemMapper.selectByExample(systemExample);
        // 将配置信息更新到数据库中
        for (MarketSystem system : systems) {
            // 通过key获得需要更新的value
            String systemKey = system.getKeyName();
            // 此systemValue为boolean类型，需要特殊处理
            if (55 == system.getId()) {
                boolean systemValue = (boolean)map.get(systemKey);
                if(systemValue){
                    // 若为真，则向数据库中写入true
                    system.setKeyValue("true");
                } else {
                    // 若为假，则向数据库中写入false
                    system.setKeyValue("false");
                }
                // 此systemValue为String类型
            } else {
                // 将value上传到数据库
                String systemValue = (String)map.get(systemKey);
                system.setKeyValue(systemValue);
            }
            // 将更改时间上传到数据库
            system.setUpdateTime(new Date());
            systemMapper.updateByPrimaryKeySelective(system);
        }
        // 提交事务，关闭sqlSession
        sqlSession.commit();
        sqlSession.close();

        return BaseRespVo.ok();
    }

}
